function ajax(method,url,params,header) {
    return new Promise(function(resolve,reject){
        let xhr = new XMLHttpRequest()
        let _method = method.toUpperCase()
        header = header?header:'application/x-www-form-urlencoded'
        if(_method=="GET"){
            let newUrl = `${url}${changeGetPar(params,_method)}`
            xhr.open(_method,newUrl,true)
            xhr.send(null)
        }else if(_method=="POST"){
            let newParams = header != 'application/x-www-form-urlencoded'?params:changeGetPar(params,_method)
            xhr.open(_method,url,true)
            xhr.setRequestHeader("Content-Type",header)
            // console.log(newParams);
            xhr.send(newParams)
        }
        xhr.onreadystatechange = function (e) {
            if (xhr.readyState == 4) {
                if(xhr.status == 200){
                    let data =xhr.responseText
                    // cb(data)
                    resolve(data)
                }else{
                    reject(xhr.status)
                }
            }
        }
    })
   
}
function changeGetPar(params,method){
    let arr = []
    for (const key in params) {
        arr.push(`${key}=${params[key]}`)
    }
    return method=="GET"?`?${arr.join('&')}`:`${arr.join('&')}`
}

let ajaxTool = {
    ajax
}
// let obj ={
//     "name": "yu",
//     "phone":"13612345678",
//     "card": "440882xxxxxxxxxxxx"
// }
// ajax('POST','http://shopback.bluej.cn/api/f_user/register',JSON.stringify(obj),'application/json').then(res=>{
//     console.log(res);
// })
// http://81.71.1.5:9998
export default{
    ajax
}

